package GUI;

import javax.swing.JPanel;

import logic.Creator;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import javax.swing.BoxLayout;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.util.Vector;
import javax.swing.JTextField;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;


public class CreatorPanel extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public int flows;
	public int size;
	public Creator creator;
	public Color[] cores = {Color.white,Color.red,Color.blue,Color.green,Color.yellow,Color.orange,
			Color.GRAY,Color.cyan,Color.magenta,Color.darkGray, Color.LIGHT_GRAY,
			Color.PINK, new Color(159,95,159), new Color(92,64,51),new Color(47,79,47),new Color(235,199,158)};
	static Color selectedColor = Color.red;
	public Cell[][] board;
	public int initPoint = 100;
	public int finalPoint = 200;
	private final ButtonGroup buttonGroup = new ButtonGroup();
	private JTextField textField;
	private boolean saved;
	public CreatorPanel(int size, final int flows) {
		this.creator= new Creator(size);
		this.board = new Cell[size][size];
		this.saved=false;
		final Vector<Color> vecColor = new Vector<Color>();
		for(int i =0; i< cores.length;i++)
			vecColor.add(cores[i]);
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{220, 229, 0};
		gridBagLayout.rowHeights = new int[]{300, 0};
		gridBagLayout.columnWeights = new double[]{1.0, 0.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{1.0, Double.MIN_VALUE};
		setLayout(gridBagLayout);

		BoardPanel board_panel = new BoardPanel(creator,board);
		GridBagConstraints gbc_board_panel = new GridBagConstraints();
		gbc_board_panel.insets = new Insets(0, 0, 0, 5);
		gbc_board_panel.gridx = 0;
		gbc_board_panel.gridy = 0;
		add(board_panel, gbc_board_panel);

		JPanel panel_1 = new JPanel();
		panel_1.setBackground(Color.LIGHT_GRAY);
		GridBagConstraints gbc_panel_1 = new GridBagConstraints();
		gbc_panel_1.fill = GridBagConstraints.VERTICAL;
		gbc_panel_1.gridx = 1;
		gbc_panel_1.gridy = 0;
		add(panel_1, gbc_panel_1);
		GridBagLayout gbl_panel_1 = new GridBagLayout();
		gbl_panel_1.columnWidths = new int[]{229, 0};
		gbl_panel_1.rowHeights = new int[]{216, 0, 0, 0, 0};
		gbl_panel_1.columnWeights = new double[]{1.0, Double.MIN_VALUE};
		gbl_panel_1.rowWeights = new double[]{0.0, 0.0, 1.0, 0.0, Double.MIN_VALUE};
		panel_1.setLayout(gbl_panel_1);

		JPanel panel = new JPanel();
		GridBagConstraints gbc_panel = new GridBagConstraints();
		gbc_panel.insets = new Insets(0, 0, 5, 0);
		gbc_panel.gridx = 0;
		gbc_panel.gridy = 0;
		panel_1.add(panel, gbc_panel);
		panel.setBackground(Color.LIGHT_GRAY);
		GridBagLayout gbl_panel = new GridBagLayout();
		gbl_panel.columnWidths = new int[]{0, 0, 0, 0};
		gbl_panel.rowHeights = new int[]{0, 0, 0, 0, 0, 0};
		gbl_panel.columnWeights = new double[]{0.0, 0.0, 0.0, Double.MIN_VALUE};
		gbl_panel.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		panel.setLayout(gbl_panel);


		JLabel lblColor = new JLabel("Color");
		GridBagConstraints gbc_lblColor = new GridBagConstraints();
		gbc_lblColor.insets = new Insets(0, 0, 5, 0);
		gbc_lblColor.gridx = 2;
		gbc_lblColor.gridy = 2;
		panel.add(lblColor, gbc_lblColor);

		JPanel panel_2 = new JPanel();
		panel_2.setBackground(Color.LIGHT_GRAY);
		GridBagConstraints gbc_panel_2 = new GridBagConstraints();
		gbc_panel_2.fill = GridBagConstraints.HORIZONTAL;
		gbc_panel_2.insets = new Insets(0, 0, 5, 0);
		gbc_panel_2.gridx = 0;
		gbc_panel_2.gridy = 1;
		panel_1.add(panel_2, gbc_panel_2);
		GridBagLayout gbl_panel_2 = new GridBagLayout();
		gbl_panel_2.columnWidths = new int[]{0, 0, 0, 0, 0};
		gbl_panel_2.rowHeights = new int[]{0, 0, 0};
		gbl_panel_2.columnWeights = new double[]{1.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		gbl_panel_2.rowWeights = new double[]{0.0, 0.0, Double.MIN_VALUE};
		panel_2.setLayout(gbl_panel_2);

		JButton btnSave = new JButton("Save");
		GridBagConstraints gbc_btnSave = new GridBagConstraints();
		gbc_btnSave.insets = new Insets(0, 0, 5, 5);
		gbc_btnSave.gridx = 0;
		gbc_btnSave.gridy = 0;
		panel_2.add(btnSave, gbc_btnSave);
		btnSave.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Integer[][] mat = new Integer[board.length][board.length];
				Vector<Integer> vecInit = new Vector<Integer>();
				Vector<Integer> vecFinal = new Vector<Integer>();
				boolean invalid=false;
				for(int i =0; i<board.length;i++)
					for(int j =0; j<board.length;j++)
					{
						if(board[i][j].color==Color.WHITE)
							mat[j][i]=0;
						else{
							int index = vecColor.indexOf(board[i][j].color);
							if(!vecInit.contains(index)){
								mat[j][i]=index+initPoint;
								vecInit.add(index);
							}
							else if(!vecFinal.contains(index)){
								mat[j][i]=index+finalPoint;
								vecFinal.add(index);
							}
							else{
								invalid=true;
							}

						}
					}
				if(vecFinal.size()!=flows || vecInit.size()!= flows)
					invalid=true;
				if(invalid)
				{
					JFrame frame = new JFrame();
					JOptionPane.showMessageDialog(frame,
							"Invalid Board.",
							"Inane error",
							JOptionPane.ERROR_MESSAGE);
					return;
				}
				if(textField.getText().length()<= 0 ){
					JFrame frame = new JFrame();
					JOptionPane.showMessageDialog(frame,
							"Invalid Name File.",
							"Inane error",
							JOptionPane.ERROR_MESSAGE);
					return;
				}
				try {
					creator.setMat(mat);
					creator.saveInFile(textField.getText());

					JFrame frame = new JFrame();
					JOptionPane.showMessageDialog(frame,
							"Save Successful.",
							"Inane information",
							JOptionPane.INFORMATION_MESSAGE);
					saved=true;
					GUI.frame.setContentPane(new MainPanel());
					GUI.frame.setBounds(100, 100, 450, 400);
					((JPanel)GUI.frame.getContentPane()).revalidate();
					GUI.frame.repaint();
				} catch (IOException e) {
					JFrame frame = new JFrame();
					JOptionPane.showMessageDialog(frame,
							"Error Saving File.",
							"Inane error",
							JOptionPane.ERROR_MESSAGE);
				}


			}
		});

		JButton btnCancel = new JButton("Cancel");
		btnCancel.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent arg0) {
				boolean exit = false;
				if(!saved){
					JFrame frame = new JFrame();
					int n = JOptionPane.showConfirmDialog(
							frame,
							"Would you exit witout saving?",
							"An Inane Question",
							JOptionPane.YES_NO_OPTION);

					if(n == JOptionPane.YES_OPTION)
						exit=true;
					else if(n == JOptionPane.NO_OPTION)
						exit=false;


				}
				if(exit){
					GUI.frame.setContentPane(new MainPanel());
					GUI.frame.setBounds(100, 100, 450, 400);
					((JPanel)GUI.frame.getContentPane()).revalidate();
					GUI.frame.repaint();
				}
			}
		});
		GridBagConstraints gbc_btnCancel = new GridBagConstraints();
		gbc_btnCancel.insets = new Insets(0, 0, 5, 0);
		gbc_btnCancel.gridx = 3;
		gbc_btnCancel.gridy = 0;
		panel_2.add(btnCancel, gbc_btnCancel);

		JLabel lblFileName = new JLabel("File Name");
		GridBagConstraints gbc_lblFileName = new GridBagConstraints();
		gbc_lblFileName.insets = new Insets(0, 0, 0, 5);
		gbc_lblFileName.gridx = 0;
		gbc_lblFileName.gridy = 1;
		panel_2.add(lblFileName, gbc_lblFileName);

		textField = new JTextField();
		GridBagConstraints gbc_textField = new GridBagConstraints();
		gbc_textField.gridx = 3;
		gbc_textField.gridy = 1;
		panel_2.add(textField, gbc_textField);
		textField.setColumns(10);
		for(int i = 0 ; i<flows+1;i++)
		{
			final Color c = cores[i];
			JRadioButton rdbtnColor = new JRadioButton(" (clear)  ");
			if(i!=0){
				rdbtnColor.setForeground(c);
				 
			}
			rdbtnColor.setBackground(c);
			rdbtnColor.addItemListener(new ItemListener() {

				@Override
				public void itemStateChanged(ItemEvent arg0) {
					if(arg0.getStateChange()==ItemEvent.SELECTED)
						CreatorPanel.selectedColor=c;

				}
			});
			buttonGroup.add(rdbtnColor);
			GridBagConstraints gbc_rdbtnColor = new GridBagConstraints();
			gbc_rdbtnColor.insets = new Insets(0, 0, 0, 0);
			gbc_rdbtnColor.gridx = 2;
			gbc_rdbtnColor.gridy = 3 +i;
			panel.add(rdbtnColor, gbc_rdbtnColor);
		}

	}

}
